package com.ccb.sc.member.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.member.service.SignInService;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.annotation.Limit;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.setting.service.ISettingsService;
import com.ccb.sc.common.modules.setting.vo.SignInSettingsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 签到表 前端控制器
 * </p>
 *
 * @author ken
 * @since 2020-09-29
 */
@RestController
@TokenAuth
@RequestMapping("/app/sign-in")
@Api(value = "签到", tags = "签到")
public class SignInController {

    private SignInService signInService;
    private ISettingsService settingsService;


    @GetMapping("/list")
    @ApiOperation(value = "总签到列表", notes = "总签到列表", response = LocalDate.class, responseContainer = "list")
    public ResBody<?> list(@ApiIgnore @CurrentUser TokenModel token) {
        List<LocalDate> signInList = this.signInService.findAllSignInList(token.getId());
        return ResBody.success(signInList);
    }

    @GetMapping("/sign-rule")
    @ApiOperation(value = "签到规则", notes = "签到规则", response = SignInSettingsVo.class)
    public ResBody<?> signRule() {
        SignInSettingsVo settings = this.settingsService.getSettings(SignInSettingsVo.class);
        return ResBody.success(settings);
    }

    @Limit(period = 2, count = 1)
    @PostMapping("/sign")
    @ApiOperation(value = "签到")
    public ResBody<?> sign(@ApiIgnore @CurrentUser TokenModel token) {
        int reward = this.signInService.sign(token.getId());
        return ResBody.success(reward);
    }

    @Autowired
    public void setSignInService(SignInService signInService) {
        this.signInService = signInService;
    }

    @Autowired
    public void setSettingsService(ISettingsService settingsService) {
        this.settingsService = settingsService;
    }
}

